# a function that performs Shapiro-Wilk test for normality element-wise to all matrices in a structure array
# version: Sep 2020
# ENSURE that Octave can find the function, use the "addpath" command: addpath ("/[insert the path to your Octave folder]/Octave")

# function arguments:
# name - the structure array where the matrices are stored
# each element in a matrix is indexed by i (row) and j (column), and k is the field number
# this function first reshapes all K i-jth elements into a vector (=sample) and then runs the test looping through all I x J vectors
function SW = struct_SWtest (name)
# get the number of fields in the structure array
nfs = numfields (name);
# transform the structure into a 3-dimensional tensor
k = 1;
for [val, key] = name
	T(:, :, k++) = val;
endfor
# rotate the initial tensor switching its third and second dimensions
T = flip(rotdim(T, -1, [2,3]),3);
# get the size of matrices within the structure array, handling the first field
[nr, nc] = size(name.(cell2mat(fieldnames(name)(1))));
# set an initial matrix to hold the results
SW = zeros(nr,nc);
# perform the Shapiro-Wilk test to each i-jth vector
for i = 1:nr
	for j = 1:nc
		SW(i,j) = SWtest(T(i,:,j));
	endfor
endfor
endfunction
